//马尔科夫链
//P(Wi | Wi - 1) = P(Wi -1, Wi) / P(Wi - 1)
module.exports =  class Peer{
    constructor(key){
        this.limit = 1000//总数超过1000 清理(全部值除以2)
        this.addScore = 0.2
        this.feedScore = 0.2
        this.count = 0
        this.current = Symbol('')
        this.countSymbol = Symbol('count')
        this.feedSymbol = Symbol('feed')
        this.dict = {[this.current]: {[this.countSymbol]: 1}}
        this.key = key
    }
    addAndFeed(key, feed = 0.8){
        let old = this.current
        this.add(key)
        this.set(parseFloat(feed), old, key)
    }
    add(key){
        key = key.toString()
        this._add(this.dict[this.current], key)
        this._add(this.dict, key)
        this.set(0.8, key)
        this.current = key
    }
    _add(dict, key){
        if(dict[key]){
            dict[key][this.countSymbol] += this.addScore
        }else{
            dict[key] = {[this.countSymbol]: this.addScore, [this.feedSymbol]: 0}
        }
        this.count ++
    }
    get(key){
        key = key.toString()
        if(this.dict[key]){
            let dict = []
            let base = (this.dict[key][this.countSymbol] + this.dict[key][this.feedSymbol]) / this.count
            for(let k in this.dict[key]){
                dict.push({key: k, value: ((this.dict[key][k][this.countSymbol] + this.dict[key][k][this.feedSymbol])
                     / this.count) / base})
            }
            dict.sort((a, b)=>{
                return b.value - a.value
            })
            return dict
        }
    }
    set(value, key = null, key2 = null){
        value = value || this.feedScore
        value = parseFloat(value)
        key = key.toString()
        if(this.dict[key]){
            if(key2){
                key2= key2.toString()
                if(this.dict[key][key2]) this.dict[key][key2][this.feedSymbol] += value
            }else{
                this.dict[key][this.feedSymbol] += value
            }
        }
        if(this.count > this.limit) this.halve()
    }
    //减半
    halve(){
        this.count /= 2
        for(let key in this.dict){
            this.dict[key][this.countSymbol] /= 2
            this.dict[key][this.feedSymbol] /= 2
            for(let k in this.dict[key]){
                this.dict[key][k][this.countSymbol] /= 2
                this.dict[key][k][this.feedSymbol] /= 2
            }
        }
    }
    //加倍
    double(){
        this.count *= 2
        for(let key in this.dict){
            this.dict[key][this.countSymbol] *= 2
            this.dict[key][this.feedSymbol] *= 2
            for(let k in this.dict[key]){
                this.dict[key][k][this.countSymbol] *= 2
                this.dict[key][k][this.feedSymbol] *= 2
            }
        }
    }
    //实际操作是加权重
    halveSet(value, key = null, key2 = null){
        this.halve()
        this.set(value, key = null, key2 = null)
    }
    //实际操作时减权重
    doubleSet(value, key = null, key2 = null){
        this.double()
        this.set(value, key = null, key2 = null)
    }
    addAndGet(key){
        this.add(key)
        return this.get(key)
    }
}